package com.example.Map;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class TestMap {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();

        map.put("23245014", "小美");
        map.put("23245015", "小黑");
        map.put("23245016", "小帅");
        //首先要获取到所有的键值对对象。
        //set集合中装的是键值对对象(Entry对象)
        //而Entry里面装的是键和值
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            //得到每一个键值对对象
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + "---" + value);
        }
        //method();
        //method1(map);
        //method2(map);
        //method3(map);
        //method4(map);
        //method5(map);
        //method6(map);
        //method7(map);
    }

    private static void method() {
        //创建集合并添加元素
        Map<String, String> map = new HashMap<>();

        map.put("23245014", "小美");
        map.put("23245015", "小黑");
        map.put("23245016", "小帅");
        //获取到所有的键
        Set<String> keys = map.keySet();
        //遍历set集合得到每一个键
        for (String key : keys) {
            //通过每一个键key，来获取到对应的值
            String s = map.get(key);
            System.out.println(key + "---" + s);
        }
//        23245014---小美
//        23245015---小黑
//        23245016---小帅
    }

    private static void method7(Map<String, String> map) {
        int size = map.size();
        System.out.println(size);   //3
    }

    private static void method6(Map<String, String> map) {
        boolean empty = map.isEmpty();
        System.out.println(empty);  //false
        map.clear();
        boolean empty1 = map.isEmpty();
        System.out.println(empty1); //true
        System.out.println(map);    //{}
    }

    private static void method5(Map<String, String> map) {
        boolean b1 = map.containsValue("小黑");
        boolean b2 = map.containsValue("aaa");
        System.out.println(b1); //true
        System.out.println(b2); //false
        System.out.println(map);
    }

    private static void method4(Map<String, String> map) {
        boolean b1 = map.containsKey("23245014");
        boolean b2 = map.containsKey("23245017");
        System.out.println(b1);     //true
        System.out.println(b2);     //false
        System.out.println(map);    //{23245014=小美, 23245015=小黑, 23245016=小帅}
    }

    private static void method3(Map<String, String> map) {
        map.clear();
        System.out.println(map);    //{}
    }

    private static void method2(Map<String, String> map) {
        String result1 = map.remove("23245014");
        String result2 = map.remove("23245017");
        System.out.println(result1);     //小美
        System.out.println(result2);    //null
        System.out.println(map);    //{23245015=小黑, 23245016=小帅}
    }

    private static void method1(Map<String, String> map) {
        //如果要添加的键不存在，那么会把键值对都添加到集合中
        //如果要添加的键是存在的，那么会覆盖原先的值，把原先值当做返回值进行返回。
        String s = map.put("23245016", "aaa");  //小帅
        System.out.println(s);
        System.out.println(map);//{23245014=小美, 23245015=小黑, 23245016=aaa}
    }
}
